package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.WxCommentCountBO;
import com.cskaoyan.bean.pojo.MarketComment;
import com.cskaoyan.bean.pojo.MarketCommentExample;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.pojo.MarketUserExample;
import com.cskaoyan.bean.vo.InnerOfWxCommentListVO;
import com.cskaoyan.bean.vo.UserInfo;
import com.cskaoyan.bean.vo.WxCommentListVO;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.WxCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang_XueCheng
 * @description
 * @since 2022/04/08 21:14
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public WxCommentListVO commentList(Integer valueId, Integer type, Integer limit, Integer page, Integer showType) {
        //开启分页
        PageHelper.startPage(page, limit);

        //查询出是valueId的评论marketComments
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(example);

        //遍历marketComments，重新赋值给我需要返回的InnerOfWxCommentListVO类型的参数
        //拿到内部对象数组，可以通过遍历去赋值
        ArrayList<InnerOfWxCommentListVO> list = new ArrayList<>();
        for (MarketComment marketComment : marketComments) {
            Integer userId = marketComment.getUserId();
            //抽了一个方法，通过userId去拿到UserInfo，这个需要返回的参数
            UserInfo userInfo = UserId2UserInfo(userId);
            //new一个对象，一个个赋值，最后添加到数组里面
            InnerOfWxCommentListVO innerOfWxCommentListVO = new InnerOfWxCommentListVO();
            innerOfWxCommentListVO.setAddTime(marketComment.getAddTime());
            innerOfWxCommentListVO.setAdminContent(marketComment.getAdminContent());
            innerOfWxCommentListVO.setContent(marketComment.getContent());
            innerOfWxCommentListVO.setPicList(marketComment.getPicUrls());
            innerOfWxCommentListVO.setUserInfo(userInfo);
            list.add(innerOfWxCommentListVO);
        }

        //规定动作
        PageInfo<MarketComment> pageInfo = new PageInfo<>(marketComments);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        WxCommentListVO wxCommentListVO = new WxCommentListVO(limit,page,pages, (int) total,list);
        return wxCommentListVO;
    }

    @Override
    public WxCommentCountBO count(Integer valueId, Integer type) {
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        criteria.andTypeEqualTo(type);
        List<MarketComment> allMarketComments = marketCommentMapper.selectByExample(example);

        //有图的
        MarketCommentExample example1 = new MarketCommentExample();
        MarketCommentExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andValueIdEqualTo(valueId);
        criteria1.andTypeEqualTo(type);
        criteria1.andHasPictureEqualTo(true);
        List<MarketComment> hasPicMarketComments = marketCommentMapper.selectByExample(example1);
        WxCommentCountBO wxCommentCountBO = new WxCommentCountBO(allMarketComments.size(), hasPicMarketComments.size());
        return wxCommentCountBO;
    }


    //抽出来的方法
    //根据example里面的userId去找到user的信息,最后拿到userinfo对象
    private UserInfo UserId2UserInfo(Integer userId) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria1 = marketUserExample.createCriteria();
        criteria1.andIdEqualTo(userId);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        UserInfo userInfo = new UserInfo();
        for (MarketUser marketUser : marketUsers) {
            String avatarUrl = marketUser.getAvatar();
            String nickname = marketUser.getNickname();
            userInfo.setAvatarUrl(avatarUrl);
            userInfo.setNickName(nickname);
        }
        return userInfo;
    }
}
